import Page from '../../../../vue-common/models/Page';
import BaseModule from './base-module'
import Ajax from '../../lib/ajax'

class WebsiteModule extends BaseModule{
    state={
        commodityMap: {},
        self: {}
    }
    actions={
        async getCommodityProfiles(context, ids){
            var temp = [];
            ids.forEach(elem => {
                if(context.state.commodityMap[elem] && context.state.commodityMap[elem].loaded) {
                    return;
                }else {
                    context.state.commodityMap[elem] = {
                        id: elem,
                        picture: '',
                        name: '',
                        classes: [],
                        loaded: false
                    }
                    temp.push(elem);
                }
            });
            if(temp.length == 0) return;
            var response = await Ajax.post('/api/services/app/Commodity/GetProfiles', { ids: temp });
            if(response.data.success) {
                response.data.result.forEach(elem => {
                    var item = context.state.commodityMap[elem.id];// Object.assign({ loaded: true }, elem);
                    item.loaded = true;
                    item.picture = elem.picture;
                    item.name = elem.name;
                    item.classes = elem.classes;
                });
            }
        },
        async getSelf(context){
            var response = await Ajax.get('/api/services/app/Admin/GetPersonalProfile');
            context.state.self = response.data.result;
        },
    };
    mutations={
    }
}

const websiteModule = new WebsiteModule();
export default websiteModule;